var yhydomain =" http://buz.payment.com/";
var yhytoken = localStorage.getItem("token");
if(yhytoken){
    $.ajaxSetup({
        headers :{ 'Authorization' : "Bearer"+yhytoken },
        beforeSend: function () {
            console.log('loading');
            // 加载loading
            $("body").append("<div class='yhy-loading'><div class='spinner'><div class='rect1'></div><div class='rect2'></div><div class='rect3'></div><div class='rect4'></div><div class='rect5'></div></div></div>");
        },
        complete: function () {
            console.log('done');
            $(".yhy-loading").remove();
        },
        error:function(){
            showMsg("系统错误");//弹出来的错误值
        }
    });
}else{
    window.location.href = '/login';
}

//验证码倒计时
function setTime($btn){
    var time = 60;
    var setTime = setInterval(function(){
        if(time<=0){
            $btn.removeClass("disabled").text("发送验证码");
            $btn.attr("disabled",false);
            clearInterval(setTime);
        }else{
            $btn.attr("disabled",true);
            $btn.addClass("disabled").text("重新发送" + time + "");
            time--;
        }
    },1000);
}
//发送验证码
function sendcode(phone,type, btn){
    $.ajax({
        url: "/api/tool/sms",
        data: { to: phone, type: type},
        type: 'post',
        success:function(data){

            if(data.code == 0){
                setTime(btn);
                showMsg("发送成功");
            }else {
                showMsg(data.message);
            }

        }
    });
}
//警告，提示弹窗
function showMsg(txt){
    var model = $("#errorModel");
    model.find(".modal-body .text-center").html('');
    model.modal("toggle");
    model.find(".modal-body .text-center").html(txt);
}

//过滤请求错误
function filterAjaxData(data) {
    if (data.code == 0) {
        return true;
    } else {
        switch (data.code) {
            case 20001:
            case 20002:
            case 20003:
                window.location.href = '/login';
                return;
                break;
            case 40005:
                window.location.href = '/auth/base';
                return;
                break;
            case 40006:
                window.location.href = '/auth/base';
                return;
                break;
            case 40003:
                var str = "";
                var obj = data.message;
                $.each(obj,function(i){
                    var childObj = obj[i];
                    for(var j=0;j<childObj.length;j++){
                        str += childObj[j]+"<br/>";
                    }
                });
                showMsg(str);
                return;
                break;
            default:
                showMsg(data.message);
                break;
        }
        return false;
    }
}
$(function(){
    $(".yhy-card-nav").mouseover(function (){
        $(".yhy-card-nav").show();
    }).mouseout(function () {
        $(".yhy-card-nav").hide();
    });

    $(".yhy_User").mouseover(function (){
        var nav = $(".yhy-card-nav");
        nav.show();
    });
});

//登出系统
function loginOut(){
    $.ajax({
        url: "/api/auth/loginout",
        type: 'post',
        success:function(data){
            console.log(data);
            if(filterAjaxData(data)){
                window.location.href="/login";
            // }else {
            //     showMsg(data.message);
            }

        }

    });
}
